package com.bwie.lib_core.network;

import android.util.TimeUtils;

import com.bwie.lib_core.filed.UrlFiled;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class UploadHttpRetrofitImpl implements HttpRetrofit{
    private static Retrofit retrofit = null;

    @Override
    public Retrofit getRetrofit() {
        return retrofit;
    }
    //针对文件上传的建造者
    //文件上传时不需要token
    //文件上传时不需要sign
    public static class UploadHttpRetrofitBuilder{
        private final long connectTime = 3;//连接超时30s
        private final long readTime = 3;//读取超时30s
        public UploadHttpRetrofitImpl build(){
            if (retrofit == null){
                createRetrofit();
            }
            return new UploadHttpRetrofitImpl();
        }

        private void createRetrofit() {
            HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            OkHttpClient.Builder okHttpBuilder = new OkHttpClient.Builder();
            okHttpBuilder.connectTimeout(connectTime, TimeUnit.MINUTES);
            okHttpBuilder.readTimeout(readTime,TimeUnit.MINUTES);
            okHttpBuilder.addInterceptor(loggingInterceptor);
            Retrofit.Builder builder = new Retrofit.Builder();
            //BASEURL->接口为一个公共url，文件上传另外一个公共url
            //文件服务器->存用户需求的文件
            builder.baseUrl(UrlFiled.BASEURL);
            builder.client(okHttpBuilder.build());
            builder.addCallAdapterFactory(RxJava2CallAdapterFactory.create());
            builder.addConverterFactory(GsonConverterFactory.create());
            retrofit = builder.build();

        }
    }
}
